<?php
include('../config/config.php');
include('../libs/commonlib.php');

echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
echo '<h1>generate po-file</h1>';

echo 'on basis of po-file: <select name="po_file">';
$poFiles = getFileListRecursive(substr(ROOT_PATH, 0, -1), ".po");
$poFiles = array_merge(array('none'), $poFiles);
foreach ($poFiles as $poFile) echo '<option>'.$poFile.'</option>';
echo '</select><br/>';

echo 'parse templates from folder: <select name="templates">';
$tFiles = getFileList(ROOT_PATH.'templates/');
foreach ($tFiles as $tFile) echo '<option>'.$tFile.'</option>';
echo '</select><br/>';

echo '<br/><input type="submit">';
echo '</form>';

if (isset($_POST['po_file']) and isset($_POST['templates'])) {
	
	$xslFiles = getFileListRecursive($_POST['templates'], ".xsl");
	$words = array();
	foreach ($xslFiles as $xslFile) {
		$xslFileContent = file_get_contents($xslFile);
		preg_match_all('/<gt>(.*?)<\/gt>/is', $xslFileContent, $pregOut);
		foreach ($pregOut[1] as $word) $words[] = $word;
		preg_match_all('/\[gt\]([^<>]*?)\[\/gt\]/is', $xslFileContent, $pregOut);
		foreach ($pregOut[1] as $word) $words[] = $word;
	}
	$fakePhpCode = "";
	foreach ($words as $word) $fakePhpCode .= 'echo _("'.$word.'");'.PHP_EOL;
	$fakePhpCode = '<?php'.PHP_EOL.$fakePhpCode.'?>';
	file_put_contents(ROOT_PATH.'uncommon/fake.php', $fakePhpCode);

	$phpFiles = getFileListRecursive(substr(ROOT_PATH, 0, -1), ".php");
	file_put_contents(ROOT_PATH."phplist", implode(PHP_EOL, $phpFiles));
	system('xgettext '.(($_POST['po_file']=='none')?'':'--join-existing --output="'.$_POST['po_file'].'"').' --no-location --from-code=UTF-8 --language=PHP --files-from="'.ROOT_PATH.'phplist"');
	unlink(ROOT_PATH.'phplist');
	unlink(ROOT_PATH.'uncommon/fake.php');
	
	$poFile = ($_POST['po_file']=='none')?ROOT_PATH."uncommon/messages.po":$_POST['po_file'];
	echo '<i><big>'.$poFile.' generated!</big></i>';
}

?>